package components.dialogs
{
	import SEApp.dialogs.se_alert;
	
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import classes.clt_events;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;

	

	public class clt_vkWaitDialog extends se_alert {
		private var _message2		: String;
		private var _descText		: String;
		private var _secCount		: int;
		private var _endCount		: int;
		private var _timer			: int;
		private var _banners		: uif_ecomponent;
		private var _bannerClass	: Class;
		public function clt_vkWaitDialog(parent:uif_component, message:String, a_message2: String, secCount: int, bannerClass: Class, okFunc:Function=null) {
			_bannerClass = bannerClass;
			super(parent, message, null, true, 620);
			okButton.addEvent(MouseEvent.CLICK, okFunc);
			okButton.addEvent(MouseEvent.CLICK, doClose);
			okButton.disable	= true;
			
			title.text = app.classes.locale.WAITTITLE;
			_timer = setInterval(doTimer, 1000);
			_secCount = secCount;
			_endCount = curSec + secCount;
			_descText = message;
			_message2 = a_message2;
			updateDesc();

			cancelButton.text 	= app.classes.locale.INVITEFRIENTS;
			cancelButton.removeEventListener(MouseEvent.CLICK, doClose);
			addEvent(MouseEvent.CLICK, function(e: MouseEvent): void {
				(app as Object).workspace.dispatchEvent(new Event(clt_events.INVITEDIALOGSHOW));
			}, cancelButton as EventDispatcher);
		}

		override protected function createDialogComponents():void {
			super.createDialogComponents();
			if (_bannerClass) {
				var inner: Rectangle = getInner();
				_banners = new _bannerClass(this);	
			}
		}
		
		override protected function resizeFromText(): Rectangle {
			if (_banners) _desc.y = _banners.y + _banners.height + getSpace(true);
			return super.resizeFromText();
		}
		
		private function clearTimer(): void {
			if (_timer) {
				clearInterval(_timer);
				_timer = 0;
			}
		}
		
		override public function dispose():void {
			clearTimer();
			super.dispose();
		}
		
		private function doTimerComplete(): void {
			clearTimer();
			okButton.disable = false;
			if (_message2) _desc.textField.htmlText = _message2;
		}
		
		private function doTimer(): void {
			_secCount = _endCount - curSec;
			if (_secCount <= 0) doTimerComplete();
			else updateDesc();
		}
		
		protected function get curSec(): int {
			var _date: Date = new Date();
			return Math.round(_date.time / 1000);
		}
		
		private function updateDesc(): void {
			_desc.textField.htmlText = _descText.replace('%s', uif_util.timeToStr(_secCount, 1));
		}
		
	}
}